package com.freeland.client2009.webservices.impl;

import java.util.HashMap;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.freeland.client2009.common.Constant;
import com.freeland.client2009.common.NType;
import com.freeland.client2009.common.UserType;
import com.freeland.client2009.service.ActionInfoService;
import com.freeland.client2009.service.CommonService;
import com.freeland.client2009.webservices.ActionService;
import com.freeland.core.utils.MultiIdUtils;

@WebService(endpointInterface="com.freeland.client2009.webservices.ActionService",targetNamespace="client2009.freeland.com",portName="ActionService")
@Component("actionServiceImpl")
public class ActionServiceImpl implements ActionService {

	@Autowired
	private ActionInfoService actionInfoService;
	@Autowired
	private CommonService commonService;
	
	public int SetGameDelete(String cooperationId, String gameId, String areaid, String userId) {
		//检查合作商ID
		int iRet = -9999 ;
		if (commonService.hasCooperation(cooperationId)){
			if(MultiIdUtils.matchMultiId(gameId)){
				
				if (!StringUtils.hasText(areaid) && !StringUtils.hasText(userId)) {
					iRet = -3; //地区ID和用户ID不能同时为空
				} else {
					if(StringUtils.hasText(areaid)){
						if(!MultiIdUtils.matchMultiId(areaid)) iRet = -4; //地区ID格式不合法
					} else if(StringUtils.hasText(userId)){
						if(!MultiIdUtils.matchMultiId(userId)) iRet = -5; //用户ID格式不合法
					} 
					if( iRet != -4 && iRet != -5 ){
						HashMap<String,Object> map = new HashMap<String,Object>();
						map.put("sareaids", areaid);
						map.put("suserids", userId);
						
						if(Constant.COOPERATION_XC.equals(cooperationId)){
							map.put("nUserType", Integer.valueOf(UserType.USER_XC.value()));
						}else{
							map.put("nUserType", Integer.valueOf(UserType.USER_EROOM.value()));
						}
						
						map.put("sparams", gameId);
						map.put("ntype", NType.DELETE.value());
						map.put("ncover", (Integer)1);
						map.put("nret", (Integer)0);
						
						if (actionInfoService.saveAction(map) == 1) iRet = 0; //修改成功
					}
				}
			} else {
				iRet = -2; //游戏ID格式不合法
			}
		} else {
			iRet = -1; //合作商ID不存在
		}
		return iRet;
	}

	public int SetGamePush(String cooperationId, int pushFlag, String gameId, String areaid, String userId) {
		//检查合作商ID
		int iRet = -9999 ;
		if (commonService.hasCooperation(cooperationId)){
			if(MultiIdUtils.matchMultiId(gameId)){
				if (!StringUtils.hasText(areaid) && !StringUtils.hasText(userId)) {
					iRet = -3; //地区ID和用户ID不能同时为空
				} else {
					if(StringUtils.hasText(areaid)){
						if(!MultiIdUtils.matchMultiId(areaid)) iRet = -4; //地区ID格式不合法
					} else if(StringUtils.hasText(userId)){
						if(!MultiIdUtils.matchMultiId(userId)) iRet = -5; //用户ID格式不合法
					} 
					if( iRet != -4 && iRet != -5 ){
						HashMap<String,Object> map = new HashMap<String,Object>();
						map.put("sareaids", areaid);
						map.put("suserids", userId);
						map.put("sparams", gameId);
						
						if(Constant.COOPERATION_XC.equals(cooperationId)){
							map.put("nUserType", Integer.valueOf(UserType.USER_XC.value()));
						}else{
							map.put("nUserType", Integer.valueOf(UserType.USER_EROOM.value()));
						}
					
						if( pushFlag == 1){
							map.put("ntype", NType.PUSH.value());
						} else {
							map.put("ntype", NType.ANTIPUSH.value());
						}
						map.put("ncover", (Integer)1);
						map.put("nret", (Integer)0);
						if (actionInfoService.saveAction(map) == 1) iRet = 0; //修改成功
					}
				}
			} else {
				iRet = -2; //游戏ID格式不合法
			}
		} else {
			iRet = -1; //合作商ID不存在
		}
		return iRet;
	}

	public int SetVelocity(String cooperationId, String areaid, String userId, int velocity) {
		//检查合作商ID
		int iRet = -9999 ;
		if (commonService.hasCooperation(cooperationId)){
			if( velocity > 0 || velocity < 10000 ){
				if (!StringUtils.hasText(areaid) && !StringUtils.hasText(userId)) {
					iRet = -3; //地区ID和用户ID不能同时为空
				} else {
					if(StringUtils.hasText(areaid)){
						if(!MultiIdUtils.matchMultiId(areaid)) iRet = -4; //地区ID格式不合法
					} else if(StringUtils.hasText(userId)){
						if(!MultiIdUtils.matchMultiId(userId)) iRet = -5; //用户ID格式不合法
					} 
					if( iRet != -4 && iRet != -5 ){
						HashMap<String,Object> map = new HashMap<String,Object>();
						map.put("sareaids", areaid);
						map.put("suserids", userId);
						if(Constant.COOPERATION_XC.equals(cooperationId)){
							map.put("nUserType", Integer.valueOf(UserType.USER_XC.value()));
						}else{
							map.put("nUserType", Integer.valueOf(UserType.USER_EROOM.value()));
						}
						
						map.put("nUserType", Integer.valueOf(UserType.USER_EROOM.value()));
						map.put("sparams", String.valueOf(velocity*1024));
						map.put("ntype", NType.LIMITSPEED.value());
						map.put("ncover", (Integer)1);
						map.put("nret", (Integer)0);
						if (actionInfoService.saveAction(map) == 1) iRet = 0; //修改成功
					}
				}
			} else {
				iRet = -2; //推送速度不合法
			}
		} else {
			iRet = -1; //合作商ID不存在
		}
		return iRet;
	}

}
